import router from 'umi/router';
import { message } from 'antd';
import { getLoginList, getOperationList,getBackupList,getRealTimeLogs,getAllDevices } from './services';

export default {
  namespace: 'logs',
  state: {
    loading: false,
    list: [],
    pageSize: 10,
    pageNum: 1,
    allDevices:[],
    realTimeList:[]
    
  },

  effects: {
    *getLoginList({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getLoginList, payload);
        yield put({
          type: 'save',
          payload: {
            pageNum: res.pageNum,
            pageSize: res.pageSize,
            total: res.total,
            list: res.pageData,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *getBackupList({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getBackupList, payload);
        yield put({
          type: 'save',
          payload: {
            pageNum: res.pageNum,
            pageSize: res.pageSize,
            total: res.total,
            list: res.pageData,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *getOperationList({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getOperationList, payload);
        yield put({
          type: 'save',
          payload: {
            pageNum: res.pageNum,
            pageSize: res.pageSize,
            total: res.total,
            list: res.pageData,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *getRealTimeLogs({ payload }, { call, put }) {
      try {
        yield put({
          type: 'changeLoading',
          payload: true,
        });
        const res = yield call(getRealTimeLogs, payload);
        yield put({
          type: 'save',
          payload: {
            realTimeList: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      } finally {
        yield put({
          type: 'changeLoading',
          payload: false,
        });
      }
    },

    *getAllDevices({ payload,callback }, { call, put }) {
      try {
        const res = yield call(getAllDevices, payload);
        if(res.data.length>0) {
          callback(res.data[0]);
        }
        yield put({
          type: 'save',
          payload: {
            allDevices: res.data,
          },
        });
      } catch (e) {
        message.error(e.message);
      }
    },

  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },

    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
